﻿using PeaceBlogData;
using PeaceBlogData.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace PeaceBlog.Controllers.API
{
    public class SettingsController : ApiController
    {
        private PeaceBlogEntity Context = new PeaceBlogEntity();

        // GET: api/Settings/
        [HttpGet]
        public async Task<List<Settings>> GetAllSettings()
        {
            return (await Context.GetAllSettings()).ToList();
        }

        // GET: api/Settings/{key}
        [HttpGet]
        public async Task<string> GetSetting(string key)
        {
            var setting = await Context.GetSetting(key);
            return (setting != null) ? setting.Value : "";
        }

        // PUT: api/Settings/
        [HttpPut]
        public async Task<IHttpActionResult> UpdateSetting(string key,string value)
        {
            return Ok(await Context.UpdateSetting(new Settings()
            {
                 Key = key,
                  Value= value
            }));
        }
    }
}
